#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtCharts>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QChartView* chartView = new QChartView(this);
    QChart* chart = new QChart();
    chartView->setChart(chart);

    //序列
    QLineSeries* series_0 = new QLineSeries();
    QLineSeries* series_1 = new QLineSeries();
    series_0->setName("Sin曲线");
    series_1->setName("Cos曲线");
    chart->addSeries(series_0);
    chart->addSeries(series_1);

    //QPen
    double x = 0;
    double v = 0.1;//步长
    for (int i = 0; i < 100; i++)
    {
        series_0->append(x, std::sin(x));
        series_1->append(x, std::cos(x));
        x += v;
    }

    //轴
    QValueAxis* axisX = new QValueAxis;
    axisX->setRange(0, 10);//范围
    axisX->setTitleText("时间（秒）");
    QValueAxis* axisY = new QValueAxis;
    axisY->setRange(-2, 2);//范围
    axisY->setTitleText("值");

    chart->addAxis(axisX, Qt::AlignBottom);
    chart->addAxis(axisY, Qt::AlignLeft);

    series_0->attachAxis(axisX);
    series_0->attachAxis(axisY);

    series_1->attachAxis(axisX);
    series_1->attachAxis(axisY);

    setCentralWidget(chartView);
}

MainWindow::~MainWindow()
{
    delete ui;
}
